package com.toplabs.unashu.service.impl;


import com.toplabs.unashu.dto.ConfigDto;
import com.toplabs.unashu.entity.SysConfig;
import com.toplabs.unashu.entity.User;
import com.toplabs.unashu.exception.ServiceException;
import com.toplabs.unashu.mapper.ConfigMapper;
import com.toplabs.unashu.service.ConfigService;
import com.toplabs.unashu.vo.ConfigVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * @author toplabs
 * @since 2022/11/21
 * <p>This java file was created by toplabs in 2022/11/21.
 * The following is the description information about this file:</p>
 * <p>description:</p>
 */
@Service
public class ConfigServiceImpl implements ConfigService {

    private final ConfigMapper configMapper;

    public ConfigServiceImpl(ConfigMapper configMapper) {
        this.configMapper = configMapper;
    }

    @Override
    public ConfigVO getSysDetail(String id) {
        SysConfig sysConfig = configMapper.selectById(id);
        if (sysConfig == null) {
            throw new ServiceException("暂时没有配置，请联系管理员！");
        }
        ConfigVO configVo = new ConfigVO();
        BeanUtils.copyProperties(sysConfig, configVo);
        return configVo;
    }

    @Override
    public void updateSysDetail(User user, String id, ConfigDto configDto) {
        SysConfig dbSysConfig = configMapper.selectById(id);
        if (dbSysConfig == null) {
            throw new ServiceException("需要修改的配置不存在！");
        }
        SysConfig sysConfig = new SysConfig();
        BeanUtils.copyProperties(configDto, sysConfig);

        sysConfig.setUpdateBy(user.getUsername());
        configMapper.update(sysConfig);
    }
}
